package org.jypj.dev.controller;

import org.jypj.dev.model.Menu;
import org.jypj.dev.service.IMenuService;
import org.jypj.dev.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Menu控制器
 * 
 * @author ChenYu
 *
 */
@Controller
@RequestMapping("menuController")
public class MenuController {

    @Resource
    private IMenuService menuService;
    /**
     * 跳转到菜单管理页面
     * @return
     */
    @RequestMapping(value="toMenuList.action")
	public String toMenuList(){

        return "/back/sys/menu_list";
    }

    /**
     * 菜单管理表单页面
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "toMenuForm.action")
    public String toMenuForm(String id, HttpServletRequest request) {
        if (StringUtil.isNotEmpty(id)) {
            Menu menu = menuService.selectMenuById(id);
            request.setAttribute("menu", menu);
        } else {
            request.setAttribute("menu", new Menu());
        }
        return "back/sys/menu_form";
    }
    /**
     * 查询所有的菜单数据
     * @return
     */
    @RequestMapping(value="selectDataMap.action")
    @ResponseBody
    public Map<String,Object> selectDataMap(){
        Map<String, Object> dataMap = new HashMap<>();
        int code=0;
        Menu queryMenu=new Menu();
        queryMenu.setParentId("root");
        List<Menu> menus = null;
        try {
            menus = menuService.selectAllByMenu(queryMenu);
        } catch (Exception e) {
            e.printStackTrace();
            code=-1;
        }
        dataMap.put("datas",menus);
        dataMap.put("code",code);
        return dataMap;
    }


    /**
     * 查询所有的菜单数据
     * @return
     */
    @RequestMapping(value="selectAllMenus.action")
    @ResponseBody
    public Map<String,Object> selectAllMenus(){
        Map<String, Object> dataMap = new HashMap<>();
        int code=0;
        List<Menu> menus = null;
        try {
            menus = menuService.selectAllByMenu(new Menu());
        } catch (Exception e) {
            e.printStackTrace();
            code=-1;
        }
        dataMap.put("data",menus);
        dataMap.put("code",code);
        return dataMap;
    }

    /**
     * 保存或者更新
     * @param menu
     * @return
     */
    @RequestMapping("saveOrUpdate.action")
    @ResponseBody
    public Map<String, Object> saveOrUpdate(Menu menu) {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        try {
            if (StringUtil.isNotEmpty(menu.getId())) {
                // 更新
                menuService.updateMenu(menu);
            } else {
                menuService.saveMenu(menu);
            }
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 删除用户
     *
     * @return
     */
    @RequestMapping("deleteMenu.action")
    @ResponseBody
    public Map<String, Object> deleteMenu(String id) {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        try {
            menuService.deleteMenuById(id);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        map.put("code", code);
        return map;
    }
}